package com.supreme.threadserver.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.supreme.commonboot.exception.BizException;
import com.supreme.commonentity.mysql.primary.BatchBase;
import com.supreme.commonentity.mysql.primary.BatchBlue;
import com.supreme.commonentity.mysql.primary.BatchGreen;
import com.supreme.commonentity.mysql.primary.BizOrder;
import com.supreme.commonmybatis.mapper.OrderMapper;
import com.supreme.commonmybatis.service.quick.ExtBatchBlueService;
import com.supreme.commonmybatis.service.quick.ExtBatchGreenService;
import com.supreme.commonmybatis.service.quick.ExtBatchRedService;
import com.supreme.threadserver.annotation.SupremeTaskAsync;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Future;

@Service
public class AsyncThreadService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ExtBatchRedService extBatchRedService;

    @Resource
    private ExtBatchGreenService extBatchGreenService;

    @Resource
    private ExtBatchBlueService extBatchBlueService;

    @SupremeTaskAsync
    public void asyncCreateBatchGreenException(String batchCode, String syncCode, Integer syncVersion, int expValue) {
        BatchGreen batchGreen = new BatchGreen()
                .setBatchCode(batchCode)
                .setSyncCode(syncCode)
                .setSyncVersion(syncVersion);
        extBatchGreenService.saveOrUpdate(batchGreen, Wrappers.lambdaQuery(BatchGreen.class).eq(BatchGreen::getBatchCode, batchCode));

        if (expValue > 1) {
            if (syncVersion % expValue == 0) {
                throw new BizException(String.format("syncVersion不能是 %d 的倍数", expValue));
            }
        }
    }

    public void createBatchBlueException(String batchCode, String syncCode, Integer syncVersion, int expValue) {
        BatchBlue batchBlue = new BatchBlue()
                .setBatchCode(batchCode)
                .setSyncCode(syncCode)
                .setSyncVersion(syncVersion);
        extBatchBlueService.saveOrUpdate(batchBlue, Wrappers.lambdaQuery(BatchBlue.class).eq(BatchBlue::getBatchCode, batchCode));

        if (expValue > 1) {
            throw new BizException("expValue不能大于1");
        }
    }

    @SupremeTaskAsync
    public void asyncCreateBatchBlue(String batchCode, String syncCode, Integer syncVersion) {
        BatchBlue batchBlue = new BatchBlue()
                .setBatchCode(batchCode)
                .setSyncCode(syncCode)
                .setSyncVersion(syncVersion);
        extBatchBlueService.saveOrUpdate(batchBlue, Wrappers.lambdaQuery(BatchBlue.class).eq(BatchBlue::getBatchCode, batchCode));
    }

    @SupremeTaskAsync
    public Future<List<BatchBase>> asyncCreateRandomBatchList(String batchCode, int expValue) {
//        jucTransactionService.createRandomBatchListSync(batchCode, expValue);
//        List<BatchBase> baseBatchList = jucTransactionService.getBaseBatchList(batchCode);
        List<BatchBase> baseBatchList = new ArrayList<>();
        return AsyncResult.forValue(baseBatchList);
    }

    @SupremeTaskAsync
    public Future<BizOrder> updateOrderNum(String orderCode, Integer orderNum) {
        BizOrder bizOrder = orderMapper.selectOne(Wrappers.lambdaQuery(BizOrder.class).eq(BizOrder::getOrderCode, orderCode));
        if (Objects.nonNull(orderNum)) {
            orderMapper.update(null, Wrappers.lambdaUpdate(BizOrder.class).set(BizOrder::getOrderNum, orderNum).eq(BizOrder::getOrderCode, orderCode));
        }
        return AsyncResult.forValue(bizOrder);
    }
}
